<?php

namespace App\Http\Controllers\admin;

use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Admin\Category;
use Cache, Event;

class CategoryController extends Controller
{
    protected $fields = [
        'pid'  => '',
        'path'  => '',
        'name'  => '',
    ];
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Category $cate,$id)
    {
        is_numeric($id) ? $id = (int)$id : $id = 1;
        $category = $cate->where('pid', $id)->orderby('id','ASC')->Paginate(15);
        $parent = $cate->find($id);
        return view('admin.category.index',compact('category','id','parent'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        $data = array();
        foreach($this->fields as $field => $default){
            $data[$field] = old($field,$default);
        }
        return view('admin.category.create',$data,compact('id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,Category $cate)
    {
        foreach (array_keys($this->fields) as $field) {
            $cate->$field = $request->get($field);
        }
        $cate->pid = $request->input('id');
        $cate->path = $cate->find($request->input('id'))->path.$request->input('id').',';
        $cate->save();
        event(new \App\Events\userActionEvent('\App\Models\Admin\Category', $cate->id, 1, '添加文章分类' . $cate->name));
        return redirect('/admin/category/index')->withSuccess('添加成功！');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id,Category $cate)
    {
        $category = $cate->find((int)$id);
        return view('admin.category.edit',$category,compact('id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,Category $cate, $id)
    {
        $category = $cate->where('pid',(int)$id)->count();
        if($category){
            return redirect('/admin/category/index')->withErrors('改分类含有子分类，无法更换上级分类！');
        }
        if($request->input('pid') == $request->input('id')){
            return redirect('/admin/category/index')->withErrors('所选上级分类与该分类相同，无法更换上级分类！');
        }
        $category = Category::find((int)$id);
        foreach (array_keys($this->fields) as $field) {
            $category->$field = $request->get($field);
        }
        $category->pid = $request->input('cate_id');
        $category->path = Category::find($request->input('cate_id'))->path.$request->input('cate_id').',';
        $category->save();
        event(new \App\Events\userActionEvent('\App\Models\Admin\Category', $category->id, 3, '编辑了文章分类' . $category->name));
        return redirect('/admin/category/index')->withSuccess('编辑成功！');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Category $cate,$id)
    {
        $category = $cate->where('pid',(int)$id)->count();
        if($category){
            return redirect('/admin/category/index')->withErrors('改分类含有子分类，无法删除！');
        }
        $category = Category::find((int)$id);
        $category->delete();
        return redirect()->back()
            ->withSuccess("删除成功");
    }
}
